import _ from 'lodash'
import Vue from 'vue'
import Router from 'vue-router'
import { Route } from '@/const'

Vue.use(Router)

const routes = []

// 路由util函数
// =============================================================================

function createRoute (name, component, options = {}) {
  if (component && component.hasOwnProperty('default')) component = component.default

  return _.merge({
    name,
    path: '/' + name,
    component
  }, options)
}

function createAndAddRoute (...args) {
  let route = createRoute(...args)
  routes.push(route)
  return route
}

// 添加路由
// =============================================================================

createAndAddRoute(Route.Index, require('@/pages/index'))
createAndAddRoute(Route.Demo, require('@/pages/demo'))
routes.push({
  path: '*',
  redirect: { name: Route.Index }
})

export default new Router({ routes })
